//
//  ChatViewController.m
//  moon
//
//  Created by beyself on 16/6/18.
//  Copyright © 2016年 beyself. All rights reserved.
//
#import "Utils.h"
#import "ChatViewController.h"
#import "ClassmateViewController.h"
#import "MateInfoViewController.h"
#import "UserSpaceViewController.h"

@interface ChatViewController ()

@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    negativeSpacer.width = -5;
    self.navigationItem.leftBarButtonItems = @[negativeSpacer,
                                               [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back"]
                                                                                style:UIBarButtonItemStylePlain
                                                                               target:self
                                                                               action:@selector(back)]];
    
//    UIImage* image = [UIImage imageNamed:@"icon_identity"];
    if(self.conversationType == ConversationType_GROUP) {UIImage* image = [UIImage imageNamed:@"icon_mate"];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image
                                                                              style:UIBarButtonItemStylePlain
                                                                             target:self
                                                                             action:@selector(showDetail)];
    }
}

- (void) showDetail
{
    if(self.conversationType == ConversationType_GROUP)
    {
        ClassmateViewController* subview = [[ClassmateViewController alloc] init];
        subview.mClassId = self.targetId;
        [self.navigationController pushViewController:subview animated:YES];
    }
    else
    {
        MateInfoViewController* subview = [[MateInfoViewController alloc] init];
        subview.mUserId = self.targetId;
        subview.needChat = YES;
        [self.navigationController pushViewController:subview animated:YES];
    }
}

- (void) back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void) viewWillAppear:(BOOL)animated
{
    if(self.conversationType == ConversationType_GROUP)
    {
        NSArray* array = APPDELEGATE.mMemberInfo[@"classes"];
        int count = array.count;
        for(int i = 0; i < count; i++)
        {
            NSDictionary* item = array[i];
            if([self.targetId isEqualToString:[item[@"id"] description]])
            {
                NSString* className = item[@"class_alias"];
                if([Utils isStringNullOrEmpty:className]) className = item[@"class_name"];
                self.title = className;
            }
        }
    }
    else
    {
        self.title = [self getUsername:self.targetId];
    }
}

- (NSString*) getUsername:(NSString*)userId
{
    NSDictionary* dic = (NSDictionary*)[Utils loadCustomObjectWithKey:ALL_MEMBER];
    if(!dic || !dic.count) return @"";
    
    NSDictionary* user = dic[userId];
    if(!user || !user.count) return @"";
    
    NSString* username = user[@"alias"];
    if([Utils isStringNullOrEmpty:username]) username = user[@"username"];
    
    return username;
}

- (void)didTapCellPortrait:(NSString *)userId
{
    MateInfoViewController* subview = [[MateInfoViewController alloc] init];
    subview.mUserId = userId;
    [self.navigationController pushViewController:subview animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
@end
